﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using log4net;
using BILAB.Business.History.Core;
using BILAB.Technical.History.PackageBuilder;


namespace BILAB.Business.History.UI
{
    public partial class BILABHistory : Form
    {
        #region Log4net
        private static readonly ILog log = LogManager.GetLogger("UI");
        #endregion

        TablePath _tp = new TablePath();     



        public BILABHistory()
        {
            InitializeComponent();
            log4net.Config.XmlConfigurator.Configure();
            log.Info("BILAB UI host started");
        }

        private void AdaptTablePath(ref TreeNode node, TablePath tablePath)
        {
            node.Text = tablePath.TableName + " Parent key to select = " + tablePath.ParentKey + " and Dependent Key to Delete = "+  tablePath.PrimaryKey;
            if (tablePath.Childs != null)
            {
                foreach (TablePath tp in tablePath.Childs)
                {
                    TreeNode childNode = new TreeNode(tp.TableName);
                    AdaptTablePath(ref childNode, tp);
                    node.Nodes.Add(childNode);
                }
            }
        }

        private void serverinstanceToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void analyseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bool state = false;
            #region comments
            //if (UserMenuItem.Text == "" && PasswordMenuItem.Text == "")
            //{
            //    state = _tp.ConnectToDatabase(ServerMenuItem.Text,
            //                                  dbListCombo.SelectedText,
            //                                  ServerAuthMode.Integrated,
            //                                  null, null);
            //}
            //else
            //{
            //    state = _tp.ConnectToDatabase(ServerMenuItem.Text,
            //                                  dbListCombo.SelectedText,
            //                                  ServerAuthMode.Integrated,
            //                                  UserMenuItem.Text, PasswordMenuItem.Text);                
            //}     
            #endregion

            //state = _tp.ConnectToDatabase(@".\BILAB2008", "AdventureWorksDW2008", ServerAuthMode.Integrated, null, null);
            state = _tp.ConnectToDatabase(@"localhost", "AdventureWorks", ServerAuthMode.Integrated, null, null);

            _tp.TableName = "SalesOrderHeader";
            _tp.Schema = "Sales";
            //_tp = _tp.AnalyseDatabaseBy(_tp.TableName, _tp.Schema);

            _tp = _tp.AnalyseDatabaseBy("Product", "Production");
            //_tp = _tp.AnalyseDatabaseBy("FactInternetSales", "dbo"); 
            TreeNode treeNode = new TreeNode();

            /* convert TreePath Tree into a TreeNode for display */
            this.AdaptTablePath(ref treeNode, _tp);
            DatabaseTreeView.Nodes.Add(treeNode);
        }

        private void ConnectMenu_Click(object sender, EventArgs e)
        {           
            List<string> dblist = null;
            if (UserMenuItem.Text == "" && PasswordMenuItem.Text == "")
            {
                dblist = _tp.GetDatabaseList(ServerMenuItem.Text, ServerAuthMode.Integrated, null, null);
            }
            else
            {
                dblist = _tp.GetDatabaseList(ServerMenuItem.Text, ServerAuthMode.Integrated, UserMenuItem.Text, PasswordMenuItem.Text);
            }

            
            dbListCombo.Items.Clear();
            foreach (string s in dblist)
            {
                log.Info("getting db name : " + s);
                dbListCombo.Items.Add(s);
            }
            dbListCombo.SelectedIndex = 0;            
        }

        private void UserMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void GenPackMenuItem_Click(object sender, EventArgs e)
        {
            MulticastExport mex = new MulticastExport();
            mex.Export();
            mex = null;
            MessageBox.Show("Package generated");
        }
    }
}
